/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.shop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.hk.shop.entity.product.HkShopProduct;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopCoupon;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkUserShop;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkUserCoupon;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkUserCouponService;

/**
 * 乐购用户优惠券Controller
 *
 * @author 李欣荣
 * @version 2018-09-28
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/shop/hkUserCoupon")
public class HkUserCouponController extends BaseController {

    @Autowired
    private HkUserCouponService hkUserCouponService;

    @ModelAttribute
    public HkUserCoupon get(@RequestParam(required = false) String id) {
        HkUserCoupon entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = hkUserCouponService.get(id);
        }
        if (entity == null) {
            entity = new HkUserCoupon();
        }
        return entity;
    }

    @RequiresPermissions(value = {"shop:shop:hkUserCoupon:view", "shop:shop:hkShopCoupon:view"}, logical = Logical.OR)
    @RequestMapping(value = {"list", ""})
    public String list(HkUserCoupon hkUserCoupon, HttpServletRequest request, HttpServletResponse response, Model model) {
        HkShopProduct hkShopProduct = new HkShopProduct();
        HkUserShop hkUserShop = new HkUserShop();
        hkUserShop.setId(UserUtils.getShopId());
        hkShopProduct.setShop(hkUserShop);
        hkUserCoupon.setProduct(hkShopProduct);
        Page<HkUserCoupon> page = hkUserCouponService.findPage(new Page<HkUserCoupon>(request, response), hkUserCoupon);
        model.addAttribute("page", page);
        return "hk/shop/shop/hkUserCouponList";
    }

    @RequiresPermissions(value = {"shop:shop:hkUserCoupon:view", "shop:shop:hkShopCoupon:view"}, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(HkUserCoupon hkUserCoupon, Model model) {
        model.addAttribute("hkUserCoupon", hkUserCoupon);
        return "hk/shop/shop/hkUserCouponForm";
    }

    @RequiresPermissions("shop:shop:hkUserCoupon:edit")
    @RequestMapping(value = "save")
    public String save(HkUserCoupon hkUserCoupon, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, hkUserCoupon)) {
            return form(hkUserCoupon, model);
        }
        hkUserCouponService.save(hkUserCoupon);
        addMessage(redirectAttributes, "保存乐购用户优惠券成功");
        return "redirect:" + Global.getAdminPath() + "/shop/shop/hkUserCoupon/?repage";
    }

    @RequiresPermissions("shop:shop:hkUserCoupon:edit")
    @RequestMapping(value = "delete")
    public String delete(HkUserCoupon hkUserCoupon, RedirectAttributes redirectAttributes) {
        hkUserCouponService.delete(hkUserCoupon);
        addMessage(redirectAttributes, "删除乐购用户优惠券成功");
        return "redirect:" + Global.getAdminPath() + "/shop/shop/hkUserCoupon/?repage";
    }


    //    @RequiresPermissions("shop:shop:hkUserCoupon:view")
    @RequestMapping(value = "getHkUserCounponByCouponId")
    public String getHkUserCounponByCouponId(@Param("couponId") String couponId, Model model, HkUserCoupon hkUserCoupon, HttpServletRequest request, HttpServletResponse response) {
        Page<HkUserCoupon> page = hkUserCouponService.findPageByCouponId(couponId);
        model.addAttribute("page", page);
        return "hk/shop/shop/hkUserCouponList";
    }

    /**
     * 导出优惠券数据
     *
     * @param hkUserCoupon
     * @param request
     * @param response
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions(value = {"shop:shop:hkUserCoupon:view", "shop:shop:hkShopCoupon:view"}, logical = Logical.OR)
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public String exportFile(HkUserCoupon hkUserCoupon, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            HkShopProduct hkShopProduct = new HkShopProduct();
            HkUserShop hkUserShop = new HkUserShop();
            hkUserShop.setId(UserUtils.getShopId());
            hkShopProduct.setShop(hkUserShop);
            hkUserCoupon.setProduct(hkShopProduct);
            String fileName = "用户优惠券数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<HkUserCoupon> page = hkUserCouponService.findUserCoupon(new Page<HkUserCoupon>(request, response, -1), hkUserCoupon);
            new ExportExcel("用户优惠券数据", HkUserCoupon.class).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出用户优惠券失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + adminPath + "/shop/shop/hkShopCoupon/list?repage";
    }

}